package com.spica.spicaschool.persistence

import androidx.room.ProvidedTypeConverter
import androidx.room.TypeConverter
import com.spica.spicaschool.bean.User
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import javax.inject.Inject

/**
 * 转换器
 */
@ProvidedTypeConverter
class TypeUserConverter @Inject
constructor(private val moshi: Moshi) {


    @TypeConverter
    fun fromJsonString(json: String): User? {
        val type = Types.newParameterizedType(User::class.java)
        val adapter = moshi.adapter<User>(type)
        return adapter.fromJson(json)
    }


    @TypeConverter
    fun typeToJson(user: User): String {
        val type = Types.newParameterizedType(User::class.java)
        val adapter = moshi.adapter<User>(type)
        return adapter.toJson(user)
    }

}